﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _14_Task_WheAll_WhenAny
{
    public partial class Form1 : Form
    {
        Random rd = new Random();

        public Form1()
        {
            InitializeComponent();
            
        }

        public void DoWork(ProgressBar progressBar)
        {
            for (int i = 0; i <= 100; i++)
            {
                //使用Invoke方法更新UI
                this.Invoke(new Action(() => 
                {
                    progressBar.Value = i;
                }));
                //模拟耗时操作，休眠
                Thread.Sleep(100);
            }
        }
        /// <summary>
        /// 启动任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;

            Task.Run(new Action(() =>
            {
                try
                {
                    //创建任务并执行任务
                    Task task1 = new Task(() => DoWork(progressBar1));
                    task1.Start();
                    //延时
                    Thread.Sleep(rd.Next(1,4)*1000);
                    //创建任务并执行任务
                    Task task2 = new Task(() => DoWork(progressBar2));
                    task2.Start();
                    //等待所有线程执行完毕后，再执行后续操作：按钮可用
                    Task.WhenAll(task1, task2).ContinueWith(t =>
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.button1.Enabled = true;
                        }));
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }

            }));

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button2.Enabled = false;

            Task.Run(new Action(() =>
            {
                try
                {
                    //创建任务并执行任务
                    Task task1 = new Task(() => DoWork(progressBar3));
                    task1.Start();
                    //延时
                    Thread.Sleep(rd.Next(1, 4) * 1000);
                    //创建任务并执行任务
                    Task task2 = new Task(() => DoWork(progressBar4));
                    task2.Start();
                    //等待其中一个线程执行完毕后，再执行后续操作：按钮可用
                    Task.WhenAny(task1, task2).ContinueWith(t =>
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.button2.Enabled = true;
                        }));
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }

            }));
        }
    }
}
